package com.gnuc.thoth.framework.db;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class ThothDB
{
	/**
	 * Thoth Database
	 * 
	 * Database name & Database version
	 */
	private static final String	DATABASE_NAME									= "thoth.db";
	private static final int		DATABASE_VERSION								= 1;
	/**
	 ****************************** table_HASH_CALL ******************************
	 */
	public static final String		table_HASH_CALL								= "h_Call";
	/* COLUMNS */
	public static final String		col_H_C_PKEY									= "_id";
	/**
	 ****************************** table_HASH_MSGS ******************************
	 */
	public static final String		table_HASH_MSGS								= "h_Msgs";
	/* COLUMNS */
	public static final String		col_H_M_PKEY									= "_id";
	/**
	 * Used to store the package name hash table_HASH_APPS ******************************
	 */
	public static final String		table_HASH_APPS								= "h_Apps";
	/* COLUMNS */
	public static final String		col_H_A_PKEY									= "_id";
	/**
	 * ****************************** table_TEMP_CALL ******************************
	 */
	public static final String		table_TEMP_CALL								= "t_Call";
	/* COLUMNS */
	public static final String		col_T_C_PKEY									= "_id";
	public static final String		col_T_C_FKEY									= "hash";
	public static final String		col_T_C_NUMBER									= "number";
	public static final String		col_T_C_CONTACT								= "contact";
	public static final String		col_T_C_CONTACTNAME							= "contact_name";
	public static final String		col_T_C_DATETIME								= "time";
	public static final String		col_T_C_DURATION								= "duration";
	// 0-INBOUND, 1-OUTBOUND, 2-REJECTED, 3-MISSED, 4-NOANSWER
	public static final String		col_T_C_TYPE									= "type";
	/**
	 * ****************************** table_TEMP_MSGS ******************************
	 **/
	/* TABLE */
	public static final String		table_TEMP_MSGS								= "t_Msgs";
	/* COLUMNS */
	public static final String		col_T_M_PKEY									= "_id";
	public static final String		col_T_M_FKEY									= "hash";
	public static final String		col_T_M_NUMBER									= "number";
	public static final String		col_T_M_CONTACT								= "contact";
	public static final String		col_T_M_CONTACTNAME							= "contact_name";
	public static final String		col_T_M_DATETIME								= "time";
	public static final String		col_T_M_MSG										= "message";
	// 0-IN SMS, 1-OUT SMS, 2-IN MMS, 3-OUT MMS
	public static final String		col_T_M_TYPE									= "type";
	public static final String		col_T_M_BLOB_TEXT								= "blob_text";
	public static final String		col_T_M_BLOB_DATA								= "blob_data";
	public static final String		col_T_M_BLOB_DATA_MIME						= "blob_data_mime";
	/**
	 * ****************************** table_TEMP_APPS ******************************
	 */
	public static final String		table_TEMP_APPS								= "t_Apps";
	/* COLUMNS */
	public static final String		col_T_A_PKEY									= "_id";
	public static final String		col_T_A_PKG										= "pkg";																																																																																																																																																																																																																												// col_H_A_PKEY
	public static final String		col_T_A_NAME									= "name";
	public static final String		col_T_A_UID										= "uid";
	public static final String		col_T_A_UID_NAME								= "uid_name";
	public static final String		col_T_A_UPLOAD									= "upload";
	public static final String		col_T_A_DOWNLOAD								= "download";
	public static final String		col_T_A_DAYID									= "day_id";
	public static final String		col_T_A_BOOTID									= "boot_id";
	/**
	 * Used to store the package name hash table_TEMP_DATA_MOBL ******************************
	 */
	public static final String		table_TEMP_DATA_MOBL							= "t_Data_Mobile";
	/* COLUMNS */
	public static final String		col_T_D_M_PKEY									= "_id";
	public static final String		col_T_D_M_DAYID								= "day_id";
	public static final String		col_T_D_M_BOOTID								= "boot_id";
	public static final String		col_T_D_M_UPLOAD								= "upload";
	public static final String		col_T_D_M_DOWNLOAD							= "download";
	public static final String		col_T_D_M_TIME									= "time";
	/**
	 * Used to store the package name hash table_TEMP_DATA_WIFI ******************************
	 */
	public static final String		table_TEMP_DATA_WIFI							= "t_Data_Wifi";
	/* COLUMNS */
	public static final String		col_T_D_W_PKEY									= "_id";
	public static final String		col_T_D_W_DAYID								= "day_id";
	public static final String		col_T_D_W_BOOTID								= "boot_id";
	public static final String		col_T_D_W_UPLOAD								= "upload";
	public static final String		col_T_D_W_DOWNLOAD							= "download";
	public static final String		col_T_D_W_TIME									= "time";
	/**
	 * ****************************** table_APP_CALL_RANK ******************************
	 */
	public static final String		table_APP_CALL_RANK							= "a_r_Call";
	/* COLUMNS */
	public static final String		col_A_R_C_PKEY									= "_id";
	// 0 - Today, 1 - This Week, 2 - Last Week, 3 - This Month, 4 - Last Month, 5- This Year
	public static final String		col_A_R_C_FILTER								= "filter";
	public static final String		col_A_R_C_NUMBER								= "number";
	public static final String		col_A_R_C_CONTACT								= "contact";
	public static final String		col_A_R_C_DURATION							= "duration";
	public static final String		col_A_R_C_CONTACTED							= "contacted";
	// 0-INBOUND 1-OUTBOUND
	public static final String		col_A_R_C_TYPE									= "type";
	/**
	 * ****************************** table_APP_MSGS_RANK ******************************
	 */
	public static final String		table_APP_MSGS_RANK							= "a_r_Msgs";
	/* COLUMNS */
	public static final String		col_A_R_M_PKEY									= "_id";
	// 0 - Today, 1 - This Week, 2 - Last Week, 3 - This Month, 4 - Last Month, 5- This Year
	public static final String		col_A_R_M_FILTER								= "filter";
	public static final String		col_A_R_M_NUMBER								= "number";
	public static final String		col_A_R_M_CONTACT								= "contact";
	public static final String		col_A_R_M_CONTACTED							= "contacted";
	// 0-SMS INBOUND 1-SMS OUTBOUND 2-MMS INBOUND 3-MMS OUTBOUND
	public static final String		col_A_R_M_TYPE									= "type";
	/**
	 * ****************************** table_APP_DATA_RANK ******************************
	 */
	public static final String		table_APP_DATA_RANK							= "a_r_Data";
	/* COLUMNS */
	public static final String		col_A_R_D_PKEY									= "_id";
	// 0 - Today, 1 - This Week, 2 - Last Week, 3 - This Month, 4 - Last Month, 5- This Year
	public static final String		col_A_R_D_FILTER								= "filter";																																																																																																																																																																																																																											// ,
	public static final String		col_A_R_D_APP_ID								= "app_uid";
	public static final String		col_A_R_D_APP_NAME							= "app_name";
	public static final String		col_A_R_D_APP_PACKAGE						= "app_pkg";
	public static final String		col_A_R_D_QUANTITY							= "quantity";
	// 0-DOWNLOAD 1-UPLOAD
	public static final String		col_A_R_D_TYPE									= "type";
	/**
	 * /** ****************************** table_APP_CALL_STAT ******************************
	 */
	public static final String		table_APP_CALL_STAT							= "a_s_Call";
	/* COLUMNS */
	public static final String		col_A_S_C_PKEY									= "_id";
	// 0 - Today, 1 - This Week, 2 - Last Week, 3 - This Month, 4 - Last Month, 5- This Year
	public static final String		col_A_S_C_FILTER								= "filter";
	public static final String		col_A_S_C_TOTAL_OUTBOUND_CALLS			= "total_outbound_calls";
	public static final String		col_A_S_C_TOTAL_INBOUND_CALLS				= "total_inbound_calls";
	public static final String		col_A_S_C_TOTAL_PLACED_CALLS				= "total_placed_calls";
	public static final String		col_A_S_C_TOTAL_RECEIVED_CALLS			= "total_received_calls";
	public static final String		col_A_S_C_TOTAL_MISSED_CALLS				= "total_missed_calls";
	/**
	 * ****************************** table_APP_MSGS_STAT ******************************
	 */
	public static final String		table_APP_MSGS_STAT							= "a_s_Msgs";
	/* COLUMNS */
	public static final String		col_A_S_M_PKEY									= "_id";
	// 0 - Today, 1 - This Week, 2 - Last Week, 3 - This Month, 4 - Last Month, 5- This Year
	public static final String		col_A_S_M_FILTER								= "filter";
	public static final String		col_A_S_M_TOTAL_OUTBOUND_MSGS				= "total_outbound_s_msgs";
	public static final String		col_A_S_M_TOTAL_INBOUND_MSGS				= "total_inbound_s_msgs";
	public static final String		col_A_S_M_TOTAL_OUTBOUND_MSGM				= "total_outbound_m_msgs";
	public static final String		col_A_S_M_TOTAL_INBOUND_MSGM				= "total_inbound_m_msgs";
	/**
	 * ****************************** table_APP_DATA_STAT ******************************
	 */
	public static final String		table_APP_DATA_STAT							= "a_s_Data";
	/* COLUMNS */
	public static final String		col_A_S_D_PKEY									= "_id";
	// 0 - Today, 1 - This Week, 2 - Last Week, 3 - This Month, 4 - Last Month, 5- This Year
	public static final String		col_A_S_D_FILTER								= "filter";
	public static final String		col_A_S_D_WIFI_UPLOADED_DATA				= "total_wifi_uploaded_data";
	public static final String		col_A_S_D_WIFI_DOWNLOADED_DATA			= "total_wifi_downloaded_data";
	public static final String		col_A_S_D_MOBL_UPLOADED_DATA				= "total_mobile_uploaded_data";
	public static final String		col_A_S_D_MOBL_DOWNLOADED_DATA			= "total_mobile_downloaded_data";
	/**
	 * Table create scripts
	 */
	private static final String	CREATE_table_HASH_CALL						= "CREATE TABLE " + table_HASH_CALL + " (" + col_H_C_PKEY + " TEXT NOT NULL PRIMARY KEY);";
	private static final String	CREATE_table_HASH_MSGS						= "CREATE TABLE " + table_HASH_MSGS + " (" + col_H_M_PKEY + " TEXT NOT NULL PRIMARY KEY);";
	private static final String	CREATE_table_HASH_APPS						= "CREATE TABLE " + table_HASH_APPS + " (" + col_H_A_PKEY + " TEXT NOT NULL PRIMARY KEY);";
	private static final String	CREATE_table_TEMP_CALL						= "CREATE TABLE " + table_TEMP_CALL + " (" + col_T_C_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_T_C_FKEY + " TEXT UNIQUE REFERENCES " + table_HASH_CALL + "(" + col_H_C_PKEY + ")," + col_T_C_NUMBER + " TEXT," + col_T_C_CONTACT + " TEXT," + col_T_C_CONTACTNAME + " TEXT," + col_T_C_DATETIME + " INTEGER," + col_T_C_DURATION + " INTEGER," + col_T_C_TYPE + " INTEGER);";
	private static final String	CREATE_table_TEMP_MSGS						= "CREATE TABLE " + table_TEMP_MSGS + " (" + col_T_M_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_T_M_FKEY + " TEXT UNIQUE REFERENCES " + table_HASH_MSGS + "(" + col_H_M_PKEY + ")," + col_T_M_NUMBER + " TEXT," + col_T_M_CONTACT + " TEXT," + col_T_M_CONTACTNAME + " TEXT," + col_T_M_DATETIME + " INTEGER," + col_T_M_MSG + " TEXT," + col_T_M_TYPE + " INTEGER," + col_T_M_BLOB_TEXT + " BLOB," + col_T_M_BLOB_DATA + " BLOB," + col_T_M_BLOB_DATA_MIME + " TEXT);";
	private static final String	CREATE_table_TEMP_APPS						= "CREATE TABLE " + table_TEMP_APPS + " (" + col_T_A_PKEY + " INTEGER PRIMARY KEY," + col_T_A_PKG + " TEXT," + col_T_A_NAME + " TEXT," + col_T_A_UID + " INTEGER," + col_T_A_UID_NAME + " TEXT," + col_T_A_UPLOAD + " INTEGER," + col_T_A_DOWNLOAD + " INTEGER," + col_T_A_DAYID + " INTEGER," + col_T_A_BOOTID + " INTEGER);";
	private static final String	CREATE_table_TEMP_DATA_MOBL				= "CREATE TABLE " + table_TEMP_DATA_MOBL + " (" + col_T_D_M_PKEY + " INTEGER PRIMARY KEY," + col_T_D_M_DAYID + " INTEGER," + col_T_D_M_BOOTID + " INTEGER," + col_T_D_M_UPLOAD + " INTEGER," + col_T_D_M_DOWNLOAD + " INTEGER," + col_T_D_M_TIME + " INTEGER);";
	private static final String	CREATE_table_TEMP_DATA_WIFI				= "CREATE TABLE " + table_TEMP_DATA_WIFI + " (" + col_T_D_W_PKEY + " INTEGER PRIMARY KEY," + col_T_D_W_DAYID + " INTEGER," + col_T_D_W_BOOTID + " INTEGER," + col_T_D_W_UPLOAD + " INTEGER," + col_T_D_W_DOWNLOAD + " INTEGER," + col_T_D_W_TIME + " INTEGER);";
	private static final String	CREATE_table_APP_CALL_RANK					= "CREATE TABLE " + table_APP_CALL_RANK + " (" + col_A_R_C_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_A_R_C_FILTER + " INTEGER," + col_A_R_C_NUMBER + " TEXT," + col_A_R_C_CONTACT + " TEXT," + col_A_R_C_DURATION + " INTEGER," + col_A_R_C_CONTACTED + " INTEGER," + col_A_R_C_TYPE + " INTEGER);";
	private static final String	CREATE_table_APP_MSGS_RANK					= "CREATE TABLE " + table_APP_MSGS_RANK + " (" + col_A_R_M_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_A_R_M_FILTER + " INTEGER," + col_A_R_M_NUMBER + " TEXT," + col_A_R_M_CONTACT + " TEXT," + col_A_R_M_CONTACTED + " INTEGER," + col_A_R_M_TYPE + " INTEGER);";
	private static final String	CREATE_table_APP_DATA_RANK					= "CREATE TABLE " + table_APP_DATA_RANK + " (" + col_A_R_D_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_A_R_D_FILTER + " INTEGER," + col_A_R_D_APP_ID + " INTEGER," + col_A_R_D_APP_NAME + " TEXT," + col_A_R_D_APP_PACKAGE + " INTEGER," + col_A_R_D_QUANTITY + " INTEGER," + col_A_R_D_TYPE + " INTEGER);";
	private static final String	CREATE_table_APP_CALL_STAT					= "CREATE TABLE " + table_APP_CALL_STAT + " (" + col_A_S_C_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_A_S_C_FILTER + " INTEGER," + col_A_S_C_TOTAL_OUTBOUND_CALLS + " INTEGER," + col_A_S_C_TOTAL_INBOUND_CALLS + " INTEGER," + col_A_S_C_TOTAL_PLACED_CALLS + " INTEGER," + col_A_S_C_TOTAL_RECEIVED_CALLS + " INTEGER," + col_A_S_C_TOTAL_MISSED_CALLS + " INTEGER);";
	private static final String	CREATE_table_APP_MSGS_STAT					= "CREATE TABLE " + table_APP_MSGS_STAT + " (" + col_A_S_M_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_A_S_M_FILTER + " INTEGER," + col_A_S_M_TOTAL_OUTBOUND_MSGS + " INTEGER," + col_A_S_M_TOTAL_INBOUND_MSGS + " INTEGER," + col_A_S_M_TOTAL_OUTBOUND_MSGM + " INTEGER," + col_A_S_M_TOTAL_INBOUND_MSGM + " INTEGER);";
	private static final String	CREATE_table_APP_DATA_STAT					= "CREATE TABLE " + table_APP_DATA_STAT + " (" + col_A_S_D_PKEY + " INTEGER PRIMARY KEY AUTOINCREMENT," + col_A_S_D_FILTER + " INTEGER," + col_A_S_D_WIFI_UPLOADED_DATA + " INTEGER," + col_A_S_D_WIFI_DOWNLOADED_DATA + " INTEGER," + col_A_S_D_MOBL_UPLOADED_DATA + " INTEGER," + col_A_S_D_MOBL_DOWNLOADED_DATA + " INTEGER);";
	/**
	 * Table drop scripts
	 */
	private static final String	DROP_table_HASH_CALL							= "DROP TABLE IF EXISTS " + table_HASH_CALL + ";";
	private static final String	DROP_table_HASH_MSGS							= "DROP TABLE IF EXISTS " + table_HASH_MSGS + ";";
	private static final String	DROP_table_HASH_APPS							= "DROP TABLE IF EXISTS " + table_HASH_APPS + ";";
	private static final String	DROP_table_TEMP_CALL							= "DROP TABLE IF EXISTS " + table_TEMP_CALL + ";";
	private static final String	DROP_table_TEMP_MSGS							= "DROP TABLE IF EXISTS " + table_TEMP_MSGS + ";";
	private static final String	DROP_table_TEMP_APPS							= "DROP TABLE IF EXISTS " + table_TEMP_APPS + ";";
	private static final String	DROP_table_TEMP_DATA_MOBL					= "DROP TABLE IF EXISTS " + table_TEMP_DATA_MOBL + ";";
	private static final String	DROP_table_TEMP_DATA_WIFI					= "DROP TABLE IF EXISTS " + table_TEMP_DATA_WIFI + ";";
	private static final String	DROP_table_APP_CALL_RANK					= "DROP TABLE IF EXISTS " + table_APP_CALL_RANK + ";";
	private static final String	DROP_table_APP_MSGS_RANK					= "DROP TABLE IF EXISTS " + table_APP_MSGS_RANK + ";";
	private static final String	DROP_table_APP_DATA_RANK					= "DROP TABLE IF EXISTS " + table_APP_DATA_RANK + ";";
	private static final String	DROP_table_APP_CALL_STAT					= "DROP TABLE IF EXISTS " + table_APP_CALL_STAT + ";";
	private static final String	DROP_table_APP_MSGS_STAT					= "DROP TABLE IF EXISTS " + table_APP_MSGS_STAT + ";";
	private static final String	DROP_table_APP_DATA_STAT					= "DROP TABLE IF EXISTS " + table_APP_DATA_STAT + ";";
	/**
	 * Table clear scripts
	 */
	public static final String		CLEAR_table_HASH_CALL						= "DELETE FROM " + table_HASH_CALL + ";";
	public static final String		CLEAR_table_HASH_MSGS						= "DELETE FROM " + table_HASH_MSGS + ";";
	public static final String		CLEAR_table_HASH_APPS						= "DELETE FROM " + table_HASH_APPS + ";";
	public static final String		CLEAR_table_TEMP_CALL						= "DELETE FROM " + table_TEMP_CALL + ";";
	public static final String		CLEAR_table_TEMP_MSGS						= "DELETE FROM " + table_TEMP_MSGS + ";";
	public static final String		CLEAR_table_TEMP_APPS						= "DELETE FROM " + table_TEMP_APPS + ";";
	public static final String		CLEAR_table_TEMP_DATA_MOBL					= "DELETE FROM " + table_TEMP_DATA_MOBL + ";";
	public static final String		CLEAR_table_TEMP_DATA_WIFI					= "DELETE FROM " + table_TEMP_DATA_WIFI + ";";
	public static final String		CLEAR_table_APP_CALL_RANK					= "DELETE FROM " + table_APP_CALL_RANK + ";";
	public static final String		CLEAR_table_APP_MSGS_RANK					= "DELETE FROM " + table_APP_MSGS_RANK + ";";
	public static final String		CLEAR_table_APP_DATA_RANK					= "DELETE FROM " + table_APP_DATA_RANK + ";";
	public static final String		CLEAR_table_APP_CALL_STAT					= "DELETE FROM " + table_APP_CALL_STAT + ";";
	public static final String		CLEAR_table_APP_MSGS_STAT					= "DELETE FROM " + table_APP_MSGS_STAT + ";";
	public static final String		CLEAR_table_APP_DATA_STAT					= "DELETE FROM " + table_APP_DATA_STAT + ";";
	private SQLiteDatabase			db;
	
	public SQLiteDatabase getDb()
	{
		return db;
	}
	
	public void setDb(SQLiteDatabase db)
	{
		this.db = db;
	}
	
	private final Context	dbContext;
	private DBHelper			dbHelper;
	
	public ThothDB(Context ctx)
	{
		dbContext = ctx;
		dbHelper = new DBHelper(dbContext, DATABASE_NAME, null, DATABASE_VERSION);
		try
		{
			db = dbHelper.getWritableDatabase();
		}
		catch (SQLiteException ex)
		{
			db = dbHelper.getReadableDatabase();
		}
	}
	
	public ThothDB open() throws SQLException
	{
		db = dbHelper.getWritableDatabase();
		return this;
	}
	
	public boolean checkDuplicate(String hash) throws SQLException
	{
		return false;
	}
	
	public void close()
	{
		db.close();
	}
	
	public ThothDB refreshHashTables(SQLiteDatabase db)
	{
		//ThothLog.i("All HASH_XXXX data will be refreshed.");
		db.execSQL(CLEAR_table_HASH_CALL);
		db.execSQL(CLEAR_table_HASH_MSGS);
		db.execSQL(CLEAR_table_HASH_APPS);
		return this;
	}
	
	public ThothDB refreshTempTables(SQLiteDatabase db)
	{
		//ThothLog.i("All TEMP_XXXX data will be refreshed.");
		db.execSQL(CLEAR_table_TEMP_CALL);
		db.execSQL(CLEAR_table_TEMP_MSGS);
		db.execSQL(CLEAR_table_TEMP_APPS);
		db.execSQL(CLEAR_table_TEMP_DATA_WIFI);
		db.execSQL(CLEAR_table_TEMP_DATA_MOBL);
		return this;
	}
	
	public ThothDB refreshRankTables(SQLiteDatabase db)
	{
		//ThothLog.i("All APP_XXX_RANK data will be refreshed.");
		db.execSQL(CLEAR_table_APP_CALL_RANK);
		db.execSQL(CLEAR_table_APP_MSGS_RANK);
		db.execSQL(CLEAR_table_APP_DATA_RANK);
		return this;
	}
	
	public ThothDB refreshStatsTables(SQLiteDatabase db)
	{
		//ThothLog.i("All APP_XXX_STAT data will be refreshed.");
		db.execSQL(CLEAR_table_APP_CALL_STAT);
		db.execSQL(CLEAR_table_APP_MSGS_STAT);
		db.execSQL(CLEAR_table_APP_DATA_STAT);
		return this;
	}
	
	public ThothDB refreshAppTable(SQLiteDatabase db)
	{
		db.execSQL(CLEAR_table_APP_CALL_RANK);
		db.execSQL(CLEAR_table_APP_DATA_RANK);
		db.execSQL(CLEAR_table_APP_MSGS_RANK);
		db.execSQL(CLEAR_table_APP_CALL_STAT);
		db.execSQL(CLEAR_table_APP_MSGS_STAT);
		db.execSQL(CLEAR_table_APP_DATA_STAT);
		return this;
	}
	
	public ThothDB refreshAllTables(SQLiteDatabase db)
	{
		//ThothLog.i("All TABLE data will be refreshed.");
		db.execSQL(CLEAR_table_HASH_CALL);
		db.execSQL(CLEAR_table_HASH_MSGS);
		db.execSQL(CLEAR_table_HASH_APPS);
		db.execSQL(CLEAR_table_TEMP_CALL);
		db.execSQL(CLEAR_table_TEMP_MSGS);
		db.execSQL(CLEAR_table_TEMP_APPS);
		db.execSQL(CLEAR_table_TEMP_DATA_MOBL);
		db.execSQL(CLEAR_table_TEMP_DATA_WIFI);
		//
		db.execSQL(CLEAR_table_APP_CALL_RANK);
		db.execSQL(CLEAR_table_APP_MSGS_RANK);
		db.execSQL(CLEAR_table_APP_DATA_RANK);
		db.execSQL(CLEAR_table_APP_CALL_STAT);
		db.execSQL(CLEAR_table_APP_MSGS_STAT);
		db.execSQL(CLEAR_table_APP_DATA_STAT);
		return this;
	}
	
	private static class DBHelper extends SQLiteOpenHelper
	{
		public DBHelper(Context context, String name, CursorFactory factory, int version)
		{
			super(context, name, factory, version);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			db.execSQL(CREATE_table_HASH_CALL);
			db.execSQL(CREATE_table_HASH_MSGS);
			db.execSQL(CREATE_table_HASH_APPS);
			db.execSQL(CREATE_table_TEMP_CALL);
			db.execSQL(CREATE_table_TEMP_MSGS);
			db.execSQL(CREATE_table_TEMP_APPS);
			db.execSQL(CREATE_table_TEMP_DATA_MOBL);
			db.execSQL(CREATE_table_TEMP_DATA_WIFI);
			//
			db.execSQL(CREATE_table_APP_CALL_RANK);
			db.execSQL(CREATE_table_APP_MSGS_RANK);
			db.execSQL(CREATE_table_APP_DATA_RANK);
			db.execSQL(CREATE_table_APP_CALL_STAT);
			db.execSQL(CREATE_table_APP_MSGS_STAT);
			db.execSQL(CREATE_table_APP_DATA_STAT);
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
		{
			//ThothLog.w("Upgrading from version " + oldVersion + " to " + newVersion + ". All TABLE data will be deleted.");
			db.execSQL(DROP_table_HASH_CALL);
			db.execSQL(DROP_table_HASH_MSGS);
			db.execSQL(DROP_table_HASH_APPS);
			db.execSQL(DROP_table_TEMP_CALL);
			db.execSQL(DROP_table_TEMP_MSGS);
			db.execSQL(DROP_table_TEMP_APPS);
			db.execSQL(DROP_table_TEMP_DATA_MOBL);
			db.execSQL(DROP_table_TEMP_DATA_WIFI);
			//
			db.execSQL(DROP_table_APP_CALL_RANK);
			db.execSQL(DROP_table_APP_MSGS_RANK);
			db.execSQL(DROP_table_APP_DATA_RANK);
			db.execSQL(DROP_table_APP_CALL_STAT);
			db.execSQL(DROP_table_APP_MSGS_STAT);
			db.execSQL(DROP_table_APP_DATA_STAT);
			onCreate(db);
		}
	}
}
